/**
 * Created by Administrator on 2016/7/8.
 */
/*******手机号码验证*******/
console.log($("#isNew").val())
$("#phone").focusout(function(){	
	phoneTest();
	
});
function phoneTest(){
	var reg = /^1[3|4|5|7|8]\d{9}$/;
	var shoujihm = $("#phone").val();
	 if(reg.test(shoujihm) && shoujihm.length == 11){
         showperson();
     } else {
    	 notifyWarning('请填写正确的手机号码');
    	 return false;
     }
}
$('#phone').focus();
var kehuxx = null;
function showperson(){
	$("#isNew").val("false");
    var shoujihm = parseInt($("#phone").val());
    var kehuxx = getKehuxxByShoujihm(shoujihm);
    if (kehuxx != '') {// 改客户存在
    	$.ajax({
    		type:"POST",
            //获取json文件路径
            url:"./yuangong/validataZhesuoShoujihm",
            //传送数据
            data:{shoujihm: shoujihm},
            //请求成功完成后要执行的方法
            success: function(data){
    			if(data.msg=="true") {
    				//表单提示--warning
					notifyWarning('该诊所已经存在该员工');
    				$("#btnSubmit").attr("disabled", true);
    				return false;
    			} else{
    				$("#btnSubmit").removeAttr("disabled");
                    $("#isNew").val("false");
                    //设置姓名
                    $("#xingming").val(kehuxx.xingming);
                    $("#xingming").attr('readOnly','readOnly');
                    //隐藏密码
                    if($("#xingming").val() !=""){
                        $(".have").remove();
                    }
    			}
    		}
    	});
	} else {
		if (kehuxx.zhuangtai == 4) {
			$('#hizhuangtai').val(4);
		}
		$("#isNew").val("ture");
    	$("#btnSubmit").removeAttr("disabled");
    	$('.z-msg').html("");
	}
};
/**************点击保存提交表单***************/   
$('#btnSubmit').click(function(){
	var keshi = $('#keshi').text();
	if(keshi == '请选择'){
		notifyWarning('请填写科室');
		return false;
	}
	phoneTest();
$('#addinfo').validator({
	stopOnError: "true",
		fields: {
	       'shoujihm': '手机号码:required;mobile;length[~25]',
	       'xingming': '姓名:required;length[~25]',
	       'mima': '密码:required;:password;',
	       'againmima': 'required;match(mima);',
	       'quanxian': '权限:checked', 
	   },
	   valid: function(form){
	   	//保存成功提示
	   	$.ajax({
			url:path+'/yuangong/xinjianyg',
			type:'POST',
			async:false,
			data:$('#addinfo').serializeArray(),
			success:function(data) {
	   			triggerTongzhifs();
				if(data=="true") {
					var msg="员工信息保存成功";
					msg=encodeURI(encodeURI(msg));
					var sjc=new Date().getTime();
					$("#cedan").load(path+"/views/zhensuo/yuangongxx.jsp?msg="+msg+"&sjc="+sjc);
					return false; 
				}else{
					var msg="员工信息保存失败";
					msg=encodeURI(encodeURI(msg));
					var sjc=new Date().getTime();
					$("#cedan").load(path+"/views/zhensuo/yuangongxx.jsp?msg="+msg+"&sjc="+sjc);
					return false; 
				}
			}
		});
	   }
	});
});	
$('#phone').keyup(function () {
	$('#btnSubmit').attr('disabled',false);
});

/*******权限*******/
$("[name='quanxian']").hover(function(){
	$(this).popover('show');},function(){
		$(this).popover('hide');
});
	  
/********取消隐藏验证*******/
$('form').validator({
	ignore:"#mima",
	ignore:"#mima1",
});
	  
/********radio验证********/
$(".f-bz").on("click",function(){
	var radioId= $(this).attr("name");
	$(this).addClass("on").siblings().removeClass("on");
	if($(this).hasClass('on')){
		$("input[type='radio'][name='jinyongbz']").prop("checked",false); 
		$("#" + radioId).prop("checked",true);
	}
});
	        
/********checkbox验证******/
$('.g-form-radio .on-check1').on("click",function(){
	var radioId= $(this).attr("for");
	if( $(this).hasClass('c-check2')){
		$(this).removeClass("c-check2");
		}
		else{
			$(this).addClass("c-check2");
		};
  });
$("#keshi").click(function(){
	$.post(path+"/keshi/getZSKeshixxByZsId",{},function(result){
		if(result.succsess) {
			var content = '';
			$(result.data).each(function(){
				content += '<li value="'+this.id+'">'+this.keshimc+'</li>';
			});
			$(".keshiul").html(content);
		}
	});
});
//科室下拉
$(document).on('click','.keshiul li',function(){
	$("#hikeshiid").val($(this).attr("value"));
	$(this).parent().parent().find('#keshi').text($(this).text());
	$(this).parent().css('display','none');
})
